package com.geek.factory.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geek.factory.entity.Menu;
import com.geek.factory.entity.Meta;
import com.geek.factory.vo.MenuVO;
import com.github.yulichang.base.MPJBaseMapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author author
 * @since 2025-06-23
 */
public interface MenuMapper extends MPJBaseMapper<Menu> {
    //根据用户ID查询一级菜单
    @Select("select m.* \n" +
            "from user u join user_role ur join role r join role_menu rm join menu m \n" +
            "on u.id = ur.user_id and ur.role_id = r.id and r.id = rm.role_id and rm.menu_id = m.id \n" +
            "where u.id = #{uid} and m.level = 1")
    @Results({
            @Result(id = true , property = "id" , column = "id"),
            @Result(column = "meta_id" , property = "meta" , javaType = Meta.class ,
                    one = @One(select = "com.geek.factory.mapper.MetaMapper.selectById"))
    })
    public List<Menu> selectFirstByUid(Long uid);


    //根据父项ID，查询对应的二级菜单
    @Select("select m2.* \n" +
            "from menu m1 join menu m2 \n" +
            "on m1.id = m2.parentId \n" +
            "where m1.id = #{id}")
    @Results({
            @Result(column = "meta_id" , property = "meta" , javaType = Meta.class ,
                    one = @One(select = "com.geek.factory.mapper.MetaMapper.selectById"))
    })
    public List<MenuVO> selectSecondById(Integer id);
}
